﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using eCommMongo.Core;
using eCommMongo.Core.Domain.Products;
using eCommMongo.Service.Pictures;
using eCommMongo.Service.Products;
using eCommMongo.Service.Securitys;
using eCommMongo.Web.Models.ProductVariants;
using eCommMongo.Web.Models.Products;

namespace eCommMongo.Web.Controllers
{

 
    public class ProductController : BaseController
    {

        private IPermissionService _permissionService;
        private IProductSearchService _productSearchService;
        private IProductService _productService;
        private IPictureService _pictureService;
        private IWorkContext _workContext;

        public ProductController(IWorkContext workContext, IPermissionService permissionService, IProductSearchService productSearchService, IProductService productService, IPictureService pictureService)
        {
            _workContext = workContext;
            _permissionService = permissionService;
            _productSearchService = productSearchService;
            _productService = productService;
            _pictureService = pictureService;
        }

        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        public ActionResult List()
        {


            var shopId = this.GetShopId();
            var products = _productSearchService.SearchProducts(shopId, string.Empty, string.Empty, string.Empty, ProductSortingEnum.CreatedOn, 0, 30);
            var model = new ProductSearchModel()
            {

                ProductListModels = ToProductListModel(products)
            };
            ViewBag.Model = model;

            return View();
        }

        public ActionResult Create()
        {
            var model = new ProductModel()
            {

                PicUrl = _pictureService.GetDefaultPictureUrl(100)
            };


            ViewBag.Model = model;

            return View();
        }

        public ActionResult Edit(string productId, string tab)
        {
            //if (!_permissionService.Authorize(StandardPermissionProvider.ManageProduct))
            //{
            //    return AccessDeniedView();
            //}

            var p = _productService.GetProductById(productId);

            var model = new ProductModel()
            {

                Id = p.Id.ToString(),
                ProductName = p.ProductName,
                ShortDescription = p.ShortDescription,
                FullDescription = p.FullDescription,

                ProductPictures = ToProductPictureModels(p.ProductPictures),
                IsPublished = p.Published,

                ProductVariants = ToProductVariantModels(p.ProductVariants),
                //ProductSpecificationAttributes=ToProductSpecificationAttributes(p.ProductSpecificationAttributes),


                DefaultUploadPicture = new ProductPictureModel()
                {
                    PictureUrl = _pictureService.GetDefaultPictureUrl(100)
                }
            };

            ViewBag.Model = model;
            ViewBag.Tab = string.IsNullOrEmpty(tab) ? "pInfo" : tab;
            return View("Edit");

        }

        private IList<ProductListModel> ToProductListModel(IList<Product> products)
        {
            if (products == null)
            {
                return null;
            }
            else
            {
                return products.Select(c => new ProductListModel()
                {
                    ProductId = c.Id,
                    IsPublished = c.Published,
           

                    ProductName = c.ProductName,
                    ProductListItemProductPicture = c.ProductPictures == null || c.ProductPictures.Count == 0
                                                        ? new ProductListProductPictureModel()
                                                        {
                                                            PicUrl = _pictureService.GetDefaultPictureUrl(50)
                                                        }
                                                        : new ProductListProductPictureModel()
                                                        {
                                                            PicUrl =
                                                                _pictureService.GetPictureUrl(
                                                                    c.ProductPictures[0].Picture.Id, 50, true)
                                                        }
                }).ToList();
            }
           
            return null;
        }
        //private IList<ProductSpecificationAttributeListItemModel> ToProductSpecificationAttributes(
        //    ICollection<ProductSpecificationAttribute> productSpecificationAttributes)
        //{
        //    if (productSpecificationAttributes == null)
        //    {
        //        return new List<ProductSpecificationAttributeListItemModel>();
        //    }
        //    else
        //    {
        //        return productSpecificationAttributes.Select(c => new ProductSpecificationAttributeListItemModel()
        //            {
        //                SpecificationAttributeName = c.SpecificationAttributeOption.SpecificationAttribute.Name,
        //                SpecificationAttributeOptionName = c.SpecificationAttributeOption.Name
        //            }).ToList();
        //    }
        //}

        public IList<ProductPictureModel> ToProductPictureModels(ICollection<ProductPicture> productPictures)
        {

            if (productPictures == null)
            {
                return new List<ProductPictureModel>();
            }
            else
            {
                return productPictures.Select(c => new ProductPictureModel()
                {
                    ProductPictureId = c.Id,
                    PictureUrl = _pictureService.GetPictureUrl(c.Picture.Id, 100, true),
                    PictureId = c.Picture.Id,
                    DisplayOrder = c.DisplayOrder,
                }).ToList();
            }
        }

        public IList<ProductVariantModel> ToProductVariantModels(ICollection<ProductVariant> productVariants)
        {

            if (productVariants == null)
            {
                return new List<ProductVariantModel>();
            }
            else
            {
                return productVariants.Select(c => new ProductVariantModel()
                {
                    Id = c.Id,
                    Name = c.Name,
                    Sku = c.Sku,
                    Price = c.Price,
                    StockQuantity = c.StockQuantity,
                    DisplayOrder = c.DisplayOrder,
                    Published = c.Published,

                }).ToList();
            }
        }
    }
}
